package com.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author Aiden
 * @version 1.0
 * @description
 * @date 2019-12-8 22:46:15
 */
@Controller
@RequestMapping("/img")
public class ImgController {
    //接受前台上传的图片
    @RequestMapping("/aimg")
    @ResponseBody
    public boolean acceptImg(MultipartRequest request) throws IOException {
        List<MultipartFile> upimgList = request.getFiles("upimg");
        for (MultipartFile img : upimgList) {
            String originalFilename = img.getOriginalFilename();
            String endName = originalFilename.substring(originalFilename.indexOf("."));
            String imgName = UUID.randomUUID().toString();
            String imgFullName = imgName + endName;
            String pathDate = new SimpleDateFormat("yyyy/MM/dd/HH/mm/ss").format(new Date());
            File fullPathOfImg = new File("G:/upLoadFile/" + pathDate, imgFullName);
            if(!fullPathOfImg.exists()){
                fullPathOfImg.mkdirs();
            }
            img.transferTo(fullPathOfImg);
        }
        return true;
    }
}
